<%@ page language="java" import="weaver.conn.RecordSet" pageEncoding="UTF-8" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.conn.RecordSetDataSource" %>
<%@ page import="weaver.general.TimeUtil" %>
<%@ page import="java.util.*" %>
<%@ page import="weaver.formmode.setup.ModeRightInfo" %>
<script>
</script>
<%
    User user = HrmUserVarify.getUser(request, response);
    int userid = user.getUID();
    int formmodeid= 137;
    RecordSet rs = new RecordSet();
    RecordSetDataSource dataSource = new RecordSetDataSource( "xfcs" );
    RecordSetDataSource dataSource1 = new RecordSetDataSource( "xfcs" );
    out.print( ">>>>>>>>开始执行>>>>>>"+"<br/>");
    try {
        dataSource.executeSql("select t.RowGuid,t.AssctID,t.AssctSumName,t.Price,t.ShiYongRenDisplayName,t.ReceiveDeptName,t1.RegisterDisplayName,\n" +
                "CONVERT(varchar(100),t1.RegisteTime, 23) as inRegisteTime,t1.WuPinLY,t1.PinPai,t1.XingHao,t1.GuiGe,t1.DengJiFS as indjfs,\n" +
                "t1.BuyerDisplayName,t1.Price,t1.BuyerOuName, CONVERT(varchar(100),t1.BuyTime, 23) as BuyTime,t1.CaiGouDisplayName,\n" +
                "CONVERT(varchar(100),t1.CaiGouTime, 23) as CaiGouTime,t1.BuyFromCompany,t1.Count,t3.OUName as SSDW,t1.Memo as inMemo,\n" +
                "CONVERT(varchar(100),t2.RegisteTime, 23) as outRegisteTime,t2.DengJiFS as outdjfs,t2.TakerUserDisplayName,\n" +
                "t2.TakerOuName,CONVERT(varchar(100),t2.TakeTime, 23) as TakeTime,t2.ShiYongRenDisplayName,\n" +
                "t2.ReceiveDeptName,t2.InAddress,t2.PublicAddress,t2.WaiDanWei,t2.fujian,t4.OUName as JSBM,t2.Memo as outMemo \n" +
                "from View_Misc_Assct_Detail_All t \n" +
                "left join  Misc_Assct_RegisteIn t1 on t.RegistInGuid=t1.RowGuid\n" +
                "left join Misc_Assct_RegisteOut t2 on t.RegistOutGuid=t2.RowGuid \n" +
                "left join frame_ou t3 on t.SSDW = t3.OUGuid\n" +
                "left join frame_ou t4 on t.JSBM = t4.OUGuid\n" +
                "where t.AssctSumName='台式电脑'");
        int count = 0;
        int counts = dataSource.getCounts();
        out.print( ">>>>>>>>查询记录总数为："+counts+"<br/>");
        while (dataSource.next()){
            Map dataMap = new HashMap(  );
            String RowGuid = Util.null2String(dataSource.getString("RowGuid"));
            String AssctID = Util.null2String(dataSource.getString("AssctID"));
            dataMap.put("zcbh",AssctID);//资产编号
            dataMap.put("rksj",Util.null2String(dataSource.getString("inRegisteTime")));//入库时间
            dataMap.put("djr",Util.null2String(dataSource.getString("RegisterDisplayName")));//登记人
            dataMap.put("zclb",Util.null2String(dataSource.getString("AssctSumName")));//资产类别
            dataMap.put("wply",Util.null2String(dataSource.getString("WuPinLY")));//物品来源
            dataMap.put("pp",Util.null2String(dataSource.getString("PinPai")));//品牌
            dataMap.put("xh",Util.null2String(dataSource.getString("XingHao")));//型号
            dataMap.put("gg",Util.null2String(dataSource.getString("GuiGe")));//规格
            dataMap.put("sl",Util.null2String(dataSource.getString("Count")));//数量
            dataMap.put("rkdjfs",Util.null2String(dataSource.getString("indjfs")));//入库登记方式
            dataMap.put("sgr",Util.null2String(dataSource.getString("BuyerDisplayName")));//申购人
            dataMap.put("dj",Util.null2String(dataSource.getString("Price")));//单价
            dataMap.put("sgbm",Util.null2String(dataSource.getString("BuyerOuName")));//申购部门
            dataMap.put("sgsj",Util.null2String(dataSource.getString("BuyTime")));//申购时间
            dataMap.put("cgr",Util.null2String(dataSource.getString("CaiGouDisplayName")));//采购人
            dataMap.put("cgsj",Util.null2String(dataSource.getString("CaiGouTime")));//采购时间
            dataMap.put("gmcs",Util.null2String(dataSource.getString("BuyFromCompany")));//购买厂商
            dataMap.put("szdw",Util.null2String(dataSource.getString("SSDW")));//所属单位
            dataMap.put("rkbz",Util.null2String(dataSource.getString("inMemo")));//入库备注
            dataMap.put("ffsj",Util.null2String(dataSource.getString("outRegisteTime")));//发放时间
            dataMap.put("ckdjfs",Util.null2String(dataSource.getString("outdjfs")));//出库登记方式
            dataMap.put("lyr",Util.null2String(dataSource.getString("TakerUserDisplayName")));//领用人
            dataMap.put("lybm",Util.null2String(dataSource.getString("TakerOuName")));//领用部门
            dataMap.put("lysj",Util.null2String(dataSource.getString("TakeTime")));//领用时间
            dataMap.put("syr",Util.null2String(dataSource.getString("ShiYongRenDisplayName")));//使用人
            dataMap.put("sybm",Util.null2String(dataSource.getString("ReceiveDeptName")));//使用部门
            dataMap.put("szwz",Util.null2String(dataSource.getString("InAddress")));//所在位置
            dataMap.put("ggcs",Util.null2String(dataSource.getString("PublicAddress")));//公共场所
            dataMap.put("wdw",Util.null2String(dataSource.getString("WaiDanWei")));//外单位
            dataMap.put("fj","");//附件
            dataMap.put("jsbm",Util.null2String(dataSource.getString("JSBM")));//结算部门
            dataMap.put("ffbz",Util.null2String(dataSource.getString("outMemo")));//发放备注
            out.print( ">>>>>>>>dataMap："+dataMap.toString()+"<br/>");
            int dataId = insertModelData(formmodeid,userid,dataMap);
            out.print( ">>>>>>>>dataId："+dataId+"<br/>");
            if(dataId>0){
                count++;
                dataSource1.executeSql("select CONVERT(varchar(100),a.Modifyime, 23) as Modifyime,a.ShiYongRenDisplayName,a.CurrentPosition,a.Memo,b.bumen from Misc_Assct_Modify a " +
                        "left join  Misc_Assct_Detail_BG b on a.AssctGuid=b.zichanguid where a.AssctGuid='"+RowGuid+"'");
                int count1 = 0;
                int counts1 = dataSource1.getCounts();
                while (dataSource1.next()){
                    String Modifyime = Util.null2String(dataSource1.getString("Modifyime"));
                    String ShiYongRenDisplayName = Util.null2String(dataSource1.getString("ShiYongRenDisplayName"));
                    String CurrentPosition = Util.null2String(dataSource1.getString("CurrentPosition"));
                    String Memo = Util.null2String(dataSource1.getString("Memo"));
                    String bumen = Util.null2String(dataSource1.getString("bumen"));
                    Object[] params = new Object[]{dataId,Modifyime,ShiYongRenDisplayName,bumen,CurrentPosition,Memo};
                    String sql = "insert into uf_zcmxjl_dt1 (mainid,bgsj,syr,sybm,szwz,bz)values(?,?,?,?,?,?)";
                    if(rs.executeUpdate(sql,params)){
                        count1++;
                    }
                }
                out.print( "执行成功明细表1数据记录数：>>>"+count1+">>>>>>明细表1总记录数："+counts1);
                dataSource1.executeSql("select Status,ShiYongRenDisplayName,ReceiveDeptName,InAddress,Price,Memo from Misc_Assct_Detail where AssctID='"+AssctID+"'");
                int count2 = 0;
                int counts2 = dataSource1.getCounts();
                while (dataSource1.next()){
                    String Status = Util.null2String(dataSource1.getString("Status"));
                    String ShiYongRenDisplayName = Util.null2String(dataSource1.getString("ShiYongRenDisplayName"));
                    String ReceiveDeptName = Util.null2String(dataSource1.getString("ReceiveDeptName"));
                    String InAddress = Util.null2String(dataSource1.getString("InAddress"));
                    String Price = Util.null2String(dataSource1.getString("Price"));
                    String Memo = Util.null2String(dataSource1.getString("Memo"));
                    Object[] params = new Object[]{dataId,AssctID,Status,ShiYongRenDisplayName,ReceiveDeptName,InAddress,Price,Memo};
                    String sql = "insert into uf_zcmxjl_dt2 (mainid,zcbh,zt,syr,sybm,szwz,dj,bz)values(?,?,?,?,?,?,?,?)";
                    if(rs.executeUpdate(sql,params)){
                        count2++;
                    }
                }
                out.print( "执行成功明细表2数据记录数：>>>"+count2+">>>>>>明细表2总记录数："+counts1);
            }
        }
        out.print( "执行成功主表数据记录数：>>>"+count+",主表数据总记录数：>>>"+counts);
    }catch (Exception e){
        out.print("执行异常，请查看日志！！！");
    }
%>
<%!
    //将数据插入表单建模
    public static int insertModelData(int modeid, int userid, Map map){
        int dataid=0;
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        //查询该模块表名
        String sql = "select b.tablename,a.formid from modeinfo a left join workflow_bill b on a.formid=b.id where a.id=?";
        rs.executeQuery(sql,modeid);
        rs.next();
        String tablename= Util.null2String(rs.getString("tablename"));
        String uuid = UUID.randomUUID().toString();
        String insSql = "insert into "+tablename
                +"(uuid,modedatacreater,modedatacreatedate,modedatacreatetime,formmodeid)values(?,?,?,?,?)";
        Object[] params = {uuid, userid, TimeUtil.getCurrentDateString(), TimeUtil.getOnlyCurrentTimeString(), modeid};
        if(rs.executeUpdate(insSql,params)){
            rs1.executeQuery("select id from "+tablename+" where uuid=?",uuid);
            rs1.next();
            dataid= Util.getIntValue(rs1.getString("id"));
            if(dataid>0){
                //遍历数据 进行update
                String updatesql="update "+tablename+" set ";
                Set<String> keySet = map.keySet();
                for(String key : keySet){
                    updatesql+=key+"='"+map.get(key).toString()+"',";
                }
                if(updatesql.endsWith(",")){
                    updatesql=updatesql.substring(0, updatesql.length()-1);
                    updatesql+=" where id="+dataid;
                    rs.executeUpdate(updatesql);
                }
                //进行权限重构
                getNewModeRight(userid, modeid, dataid);
            }
        }
        return dataid;
    }
    //建模权限重构
    public static void getNewModeRight(int userid, int formmodeid, int id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight( true );
        modeRightInfo.editModeDataShare( userid, formmodeid, id  );
    }
%>